package com.orangelife.service.propFeeCommInfo.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.orangelife.exception.AppBizException;
import com.orangelife.service.PropFeeType.IPropFeeTypeService;
import com.orangelife.service.PropFeeType.model.PropFeeTypeDto;
import com.orangelife.service.propFeeDic.IPropFeeDicService;
import com.orangelife.utils.HttpSvcUtils;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.JsonMapUtils;
import com.orangelife.utils.UrlPath;

public class HandleFeeTypeThread extends Thread implements Runnable {
	Logger logger = Logger.getLogger(HandleFeeTypeThread.class);
	private String threadName;
	private String commID;
	private List<String> feeTypeNames;

	private int start;
	private int end;

	private IPropFeeTypeService propFeeTypeService;
	private IPropFeeDicService propFeeDicService;

	public HandleFeeTypeThread(String threadName, List<String> feeTypeNames,
			String commID, IPropFeeTypeService propFeeTypeService,
			IPropFeeDicService propFeeDicService) {
		this.threadName = threadName;
		this.feeTypeNames = feeTypeNames;

		this.propFeeTypeService = propFeeTypeService;
		this.commID = commID;
		this.propFeeDicService = propFeeDicService;

	}

	public void run() {
		try {

			Map<String, Object> parMap = new HashMap<String, Object>();
			parMap.put("itemStat", "1");
			List<Map<String, Object>> initfeeTypes = JsonMapUtils
					.getPageEntityMap(HttpSvcUtils.doPostSvc(
							UrlPath.initFeeTypes, parMap, null));
			/***
			 * 物业管理费,公共维修金,车位管理费,车位维修金,公摊水电费,其他公摊费,代收代缴,其他收费
			 */
			/*
			 * 
			 * 
			 * for (int i = 0; i < feeTypeNames.size(); i++) {
			 * System.out.println(threadName + "\t\t\t\t\t 请求 数据" + "\t\t\t\t" +
			 * feeTypeNames.get(i) + " \t\t" + commID); List<PropFeeTypeDto>
			 * pList = propFeeTypeService .findPropFeeTypeByRelevantID(null,
			 * commID, feeTypeNames.get(i), null, null, "0"); if (pList == null
			 * || pList.size() <= 0) { PropFeeTypeDto propFeeType = new
			 * PropFeeTypeDto();
			 * propFeeType.setFeeTypeName(feeTypeNames.get(i));
			 * propFeeType.setCommID(commID); propFeeType.setLevel(1);
			 * propFeeType.setFeeTypePID("0"); propFeeType.setSort(i);
			 * 
			 * 
			 * 
			 * if (feeTypeNames.get(i).equals("物业管理费") ||
			 * feeTypeNames.get(i).equals("车位管理费") ||
			 * feeTypeNames.get(i).equals("车位维修金") ||
			 * feeTypeNames.get(i).equals("代收代缴") ||
			 * feeTypeNames.get(i).equals("其他收费")) { propFeeType.setType("0"); }
			 * if (feeTypeNames.get(i).equals("公共维修金") ||
			 * feeTypeNames.get(i).equals("公摊水电费") ||
			 * feeTypeNames.get(i).equals("其他公摊费")) { propFeeType.setType("1");
			 * }
			 */
			List<PropFeeTypeDto> propFeeTypes = new ArrayList<PropFeeTypeDto>();
			List<PropFeeTypeDto> propFeePTypes = new ArrayList<PropFeeTypeDto>();
			if (initfeeTypes != null && initfeeTypes.size() > 0) {
				for (int i = 0; i < initfeeTypes.size(); i++) {
					System.out.println(threadName + "\t\t\t\t\t 请求 数据"
							+ "\t\t\t\t" + initfeeTypes.get(i) + " \t\t"
							+ commID);

					logger.info(threadName + "\t\t\t\t\t 请求 数据" + "\t\t\t\t"
							+ initfeeTypes.get(i) + " \t\t" + commID);
					List<PropFeeTypeDto> pList = propFeeTypeService
							.findPropFeeTypeByRelevantID(null, commID,
									initfeeTypes.get(i).get("feeTypeName")
											.toString(), null, null, null);
					if (pList == null || pList.size() <= 0) {
						PropFeeTypeDto propFeeType = new PropFeeTypeDto();
						propFeeType.setFeeTypeName(initfeeTypes.get(i)
								.get("feeTypeName").toString());
						propFeeType.setCommID(commID);
						propFeeType.setLevel(Integer.parseInt(initfeeTypes
								.get(i).get("level").toString()));

						propFeeType.setFeeTypePID(initfeeTypes.get(i)
								.get("feeTypePID").toString());

						propFeeType
								.setCode(initfeeTypes.get(i).get("code") != null ? initfeeTypes
										.get(i).get("code").toString()
										: null);
						propFeeType
								.setRemark(initfeeTypes.get(i).get("remark") != null ? initfeeTypes
										.get(i).get("remark").toString()
										: null);

						propFeeType.setTypeID(initfeeTypes.get(i)
								.get("feeTypeID").toString());
						propFeeType.setSort(i);
						propFeeType.setTscreate(new Date());
						propFeeType.setTsedit(new Date());
						propFeeType.setItemStat("1");
						propFeeType.setFeeTypeID(IdUtil.getuuId());
						propFeeType
								.setType((initfeeTypes.get(i).get("type") != null ? initfeeTypes
										.get(i).get("type").toString()
										: "0"));

						propFeeTypes.add(propFeeType);
					}

				}

				/***
				 * 判断是否是初次同步
				 */
				if (propFeeTypes != null && propFeeTypes.size() > 0) {
					for (int i = 0; i < propFeeTypes.size(); i++) {
						if (propFeeTypes.get(i).getFeeTypePID().equals("0")) {
							propFeePTypes.add(propFeeTypes.get(i));
						}
					}

					if (propFeePTypes != null && propFeePTypes.size() > 0) {
						for (int i = 0; i < propFeeTypes.size(); i++) {
							for (int j = 0; j < propFeePTypes.size(); j++) {
								if (propFeeTypes
										.get(i)
										.getFeeTypePID()
										.equals(propFeePTypes.get(j)
												.getTypeID())) {
									propFeeTypes.get(i)
											.setFeeTypePID(
													propFeePTypes.get(j)
															.getFeeTypeID());
								}
							}
						}
						propFeeTypeService.addPropFeeType(propFeeTypes);
					} else {
						/***
						 * 判断是否新增子类
						 */

						List<PropFeeTypeDto> pList = propFeeTypeService
								.findPropFeeTypeByRelevantID(null, commID,
										null, null, null, "0", null);
						if (pList != null && pList.size() > 0) {
							for (int i = 0; i < propFeeTypes.size(); i++) {
								for (PropFeeTypeDto propFeeTypeDto : pList) {
									if (propFeeTypes.get(i).getCode()
											.substring(0, 2)
											.equals(propFeeTypeDto.getCode())) {
										propFeeTypes.get(i).setFeeTypePID(
												propFeeTypeDto.getFeeTypeID());
									}

								}

							}

						}
						propFeeTypeService.addPropFeeType(propFeeTypes);

					}

				}

			}

		} catch (AppBizException e) {
			logger.error(e.getMessage());
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
